package cn.edu.nju.software.android.service;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import cn.edu.nju.software.android.activity.MessagesActivity;
import cn.edu.nju.software.android.activity.R;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.v4.app.NotificationCompat;

@SuppressLint("HandlerLeak")
public class MessagesService extends Service {

	public static int mention_status_id = 0;
	public static int mention_cmt_id = 1;
	public static int cmt_id = 2;
	public static int mention_status_unread = 0;
	public static int mention_cmt_unread = 0;
	public static int cmt_unread = 0;
	private IBinder binder = new MessagesService.LocalBinder();
	private Timer timer = null;

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			int mention_status = msg.getData().getInt("mention_status");
			int mention_cmt = msg.getData().getInt("mention_cmt");
			int cmt = msg.getData().getInt("cmt");

			NotificationManager mNotificationManager =
					(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

			if (mention_status > 0) {
				if (mention_status > mention_status_unread) {
					if (mention_status_unread != 0) {
						mNotificationManager.cancel(mention_status_id - 3);
					}
					NotificationCompat.Builder mBuilder =
							new NotificationCompat.Builder(MessagesService.this)
					.setSmallIcon(R.drawable.small_logo)
					.setContentTitle("新@我")
					.setContentText("您有" + mention_status + "条未读新@！");
					// Creates an explicit intent for an Activity in your app
					Intent resultIntent = new Intent(MessagesService.this, MessagesActivity.class);
					resultIntent.putExtra("type", "mention_status");
					PendingIntent resultPendingIntent = PendingIntent.getActivity(
							MessagesService.this, 
							R.string.app_name, 
							resultIntent, 
							PendingIntent.FLAG_UPDATE_CURRENT);
					mBuilder.setContentIntent(resultPendingIntent);
					Notification notification = mBuilder.build();
					notification.flags = Notification.FLAG_AUTO_CANCEL;
					// id allows you to update the notification later on.
					mNotificationManager.notify(mention_status_id, notification);
					cn.edu.nju.software.android.model.Message.clearUnreadMesssageNum("mention_status");
					mention_status_id += 3;
					mention_status_unread = mention_status;
				}
			}
			if (mention_cmt > 0) {
				if (mention_cmt > mention_cmt_unread) {
					if (mention_cmt_unread != 0) {
						mNotificationManager.cancel(mention_cmt_id - 3);
					}
					NotificationCompat.Builder mBuilder =
							new NotificationCompat.Builder(MessagesService.this)
					.setSmallIcon(R.drawable.small_logo)
					.setContentTitle("新@我")
					.setContentText("您有" + mention_cmt + "条未读新@！");
					// Creates an explicit intent for an Activity in your app
					Intent resultIntent = new Intent(MessagesService.this, MessagesActivity.class);
					resultIntent.putExtra("type", "mention_cmt");
					PendingIntent resultPendingIntent = PendingIntent.getActivity(
							MessagesService.this, 
							R.string.app_name, 
							resultIntent, 
							PendingIntent.FLAG_UPDATE_CURRENT);
					mBuilder.setContentIntent(resultPendingIntent);
					Notification notification = mBuilder.build();
					notification.flags = Notification.FLAG_AUTO_CANCEL;
					// id allows you to update the notification later on.
					mNotificationManager.notify(mention_cmt_id, notification);
					cn.edu.nju.software.android.model.Message.clearUnreadMesssageNum("mention_cmt");
					mention_cmt_id += 3;
					mention_cmt_unread = mention_cmt;
				}
			}
			if (cmt > 0) {
				if (cmt > cmt_unread) {
					if (cmt_unread != 0) {
						mNotificationManager.cancel(cmt_id - 3);
					}
					NotificationCompat.Builder mBuilder =
							new NotificationCompat.Builder(MessagesService.this)
					.setSmallIcon(R.drawable.small_logo)
					.setContentTitle("新评论我")
					.setContentText("您有" + cmt + "条未读新评论！");
					// Creates an explicit intent for an Activity in your app
					Intent resultIntent = new Intent(MessagesService.this, MessagesActivity.class);
					resultIntent.putExtra("type", "cmt");
					PendingIntent resultPendingIntent = PendingIntent.getActivity(
							MessagesService.this, 
							R.string.app_name, 
							resultIntent, 
							PendingIntent.FLAG_UPDATE_CURRENT);
					mBuilder.setContentIntent(resultPendingIntent);
					Notification notification = mBuilder.build();
					notification.flags = Notification.FLAG_AUTO_CANCEL;
					// id allows you to update the notification later on.
					mNotificationManager.notify(cmt_id, notification);
					cn.edu.nju.software.android.model.Message.clearUnreadMesssageNum("cmt");
					cmt_id += 3;
					cmt_unread = cmt;
				}
			}
		}

	};

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return binder;
	}

	@Override 
	public void onCreate() { 
		super.onCreate();
		timer = new Timer();
		timer.scheduleAtFixedRate(new TimerTask() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				cn.edu.nju.software.android.model.Message.getUnreadMessageNum(handler);
			}

		}, new Date(), 60000);
	}

	@Override 
	public void onDestroy() { 
		super.onDestroy();
	}

	public class LocalBinder extends Binder{
		MessagesService getService(){
			return MessagesService.this;
		}
	}
	
}
